package com.centit.airplaneinfo_db.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.centit.airplaneinfo_db.dao.DbRecordDao;
import com.centit.airplaneinfo_db.po.DbOldInfoDjpql;
import com.centit.airplaneinfo_db.po.DbOldInfoGcwtl;
import com.centit.airplaneinfo_db.po.DbRecord;
import com.centit.airplaneinfo_db.service.DbRecordManager;
import com.centit.framework.jdbc.service.BaseEntityManagerImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DbRecordManagerImpl extends BaseEntityManagerImpl<DbRecord, String, DbRecordDao>
        implements DbRecordManager {

    private DbRecordDao dbRecordDao ;

    @Resource(name = "dbRecordDao")
    @NotNull
    public void setDbRecordDao(DbRecordDao baseDao)
    {
        this.dbRecordDao = baseDao;
        setBaseDao(this.dbRecordDao);
    }


    @Override
    public JSONArray newDbRecordValue(List<DbOldInfoGcwtl> oldData, String recordOldId) {
        JSONArray jsonArray=dbRecordDao.newDbRecordValue(recordOldId);
        System.out.println("------jsonArray-----"+ JSON.toJSONString(jsonArray));
        Map<String,JSONObject> dataMap=new HashMap<>();
        jsonArray.stream().forEach(w->{
            JSONObject jsonObject=(JSONObject)w;
            if("2".equals(oldData.get(0).getTableType())){
                BigInteger aDjBhs=new BigInteger(jsonObject.getString("aDjBhs"));
                BigInteger aDjHs=new BigInteger(jsonObject.getString("aDjHs"));
                BigInteger flightHours=new BigInteger(jsonObject.getString("flightHours"));
                /*Integer hjBhs=jsonObject.getIntValue("aDjBhs")*jsonObject.getIntValue("flightHours");
                Integer hjhs=jsonObject.getIntValue("aDjHs")*jsonObject.getIntValue("flightHours");*/
                BigInteger hjBhs=aDjBhs.multiply(flightHours);
                BigInteger hjhs=aDjHs.multiply(flightHours);
               /* System.out.println("--------aDjBhs--------|"+aDjBhs);
                System.out.println("--------aDjHs--------|"+aDjHs);
                System.out.println("--------flightHours--------|"+flightHours);
                System.out.println("--------hjBhs--------|"+hjBhs);
                System.out.println("--------hjhs--------|"+hjhs);
                System.out.println("--------airPlaneType--------|"+jsonObject.getString("airPlaneType"));*/
                jsonObject.put("hjBhs",hjBhs);
                jsonObject.put("hjHs",hjhs);
                jsonObject.put("unitPrice",jsonObject.getIntValue("aDjBhs"));
            }else {
//                Integer hjBhs=jsonObject.getIntValue("zzjDjBhs")*jsonObject.getIntValue("flightHours");
//                Integer hjhs=jsonObject.getIntValue("zzjDjHs")*jsonObject.getIntValue("flightHours");
                BigInteger zzjDjBhs=new BigInteger(jsonObject.getString("zzjDjBhs"));
                BigInteger zzjDjHs=new BigInteger(jsonObject.getString("zzjDjHs"));
                BigInteger flightHours=new BigInteger(jsonObject.getString("flightHours"));

                BigInteger hjBhs=zzjDjBhs.multiply(flightHours);
                BigInteger hjhs=zzjDjHs.multiply(flightHours);
                jsonObject.put("hjBhs",hjBhs);
                jsonObject.put("hjHs",hjhs);
                jsonObject.put("unitPrice",jsonObject.getIntValue("zzjDjBhs"));
            }
            dataMap.put(jsonObject.getString("airPlaneType"),jsonObject);
        });
        JSONArray newDataArr=new JSONArray();
        if(jsonArray.size()>0){
            /*Integer gjFxxs=0;
            Integer gjBhs=0;
            Integer gjHs=0;*/
            BigInteger gjFxxs=BigInteger.ZERO;
            BigInteger gjBhs=BigInteger.ZERO;
            BigInteger gjHs=BigInteger.ZERO;
            for(DbOldInfoGcwtl dbOldInfoGcwtl:oldData){
                if(StringUtils.isNotEmpty(dbOldInfoGcwtl.getAirPlaneType())){
                    if(dataMap.containsKey(dbOldInfoGcwtl.getAirPlaneType())){
                        newDataArr.add(dataMap.get(dbOldInfoGcwtl.getAirPlaneType()));
                        gjFxxs = gjFxxs.add(new BigInteger(dataMap.get(dbOldInfoGcwtl.getAirPlaneType()).getString("flightHours")));
                        gjBhs = gjBhs.add(new BigInteger(dataMap.get(dbOldInfoGcwtl.getAirPlaneType()).getString("hjBhs")));
                        gjHs = gjHs.add(new BigInteger(dataMap.get(dbOldInfoGcwtl.getAirPlaneType()).getString("hjHs")));
                        /*gjFxxs+=dataMap.get(dbOldInfoGcwtl.getAirPlaneType()).getIntValue("flightHours");
                        gjBhs+=dataMap.get(dbOldInfoGcwtl.getAirPlaneType()).getIntValue("hjBhs");
                        gjHs+=dataMap.get(dbOldInfoGcwtl.getAirPlaneType()).getIntValue("hjHs");*/
                    }else {
                        JSONObject jsonObject=new JSONObject();
                        jsonObject.put("airPlaneType",dbOldInfoGcwtl.getAirPlaneType());
                        jsonObject.put("dataType","1");
                        newDataArr.add(jsonObject);
                    }
                }

            }

            JSONObject jsonObject=new JSONObject();
            jsonObject.put("airPlaneType","");
            jsonObject.put("gjFxxs",gjFxxs);
            jsonObject.put("gjBhs",gjBhs);
            jsonObject.put("gjHs",gjHs);
            jsonObject.put("dataType","2");
            newDataArr.add(jsonObject);
        }

        return newDataArr;
    }

    @Override
    public Map<String, Object> contrastData(List<DbOldInfoDjpql> oldData, List<DbOldInfoDjpql> newData) {
        Map<String, Object> resultMap=new HashMap<>();
        Map<String,String> oldMap=new HashMap<>();
        Map<String,String> oldMapJh=new HashMap<>();
        Map<String,String> newMap=new HashMap<>();
        Map<String,String> newMapJh=new HashMap<>();
        oldData.stream().forEach(w->{
            //oldMap.put(w.getAirPlaneType()+"_"+w.getAirPlaneNum()+"_"+w.getDjLx(),"1");
            oldMap.put(w.getAirPlaneNum()+"_"+w.getDjLx(),"1");
            oldMapJh.put(w.getAirPlaneNum(),"1");
        });
        newData.stream().forEach(w->{
            newMap.put(w.getAirPlaneNum()+"_"+w.getDjLx(),"1");
            newMapJh.put(w.getAirPlaneNum(),"1");
        });
        oldData.stream().forEach(w->{
            String key=w.getAirPlaneNum()+"_"+w.getDjLx();
            if(newMap.containsKey(key)){
                w.setTrType("normal");
            }else {
                //w.setTrType("different");
                if(newMapJh.containsKey(w.getAirPlaneNum())){
                    w.setTrType("normalJh");
                }else {
                    w.setTrType("different");
                }
            }

        });
        newData.stream().forEach(w->{
            String key=w.getAirPlaneNum()+"_"+w.getDjLx();
            if(oldMap.containsKey(key)){
                w.setTrType("normal");
            }else {
                //w.setTrType("different");
                if(oldMapJh.containsKey(w.getAirPlaneNum())){
                    w.setTrType("normalJh");
                }else {
                    w.setTrType("different");
                }
            }

        });
        resultMap.put("old",oldData);
        resultMap.put("new",newData);
        return resultMap;
    }
}
